package com.hxzy.shop.marketing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.shop.common.dto.marketing.PageRequest;
import com.hxzy.shop.common.dto.marketing.req.AddImageReq;
import com.hxzy.shop.entity.DistributorImage;
import com.hxzy.shop.marketing.service.DistributorImageService;
import com.hxzy.shop.marketing.mapper.DistributorImageMapper;
import com.hxzy.shop.vo.ApiException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
* @author admin
* @description 针对表【distributor_image】的数据库操作Service实现
* @createDate 2023-08-10 16:30:24
*/
@Service
public class DistributorImageServiceImpl extends ServiceImpl<DistributorImageMapper, DistributorImage>
    implements DistributorImageService{

    @Resource
    private DistributorImageMapper distributorImageMapper;
    /**
     * 分销推广图列表
     * @param req
     * @return
     */
    @Override
    public PageInfo<DistributorImage> imageList(PageRequest req) {
        return PageHelper.startPage(req.getPageNum(),req.getPageSize()).doSelectPageInfo(()->{
            distributorImageMapper.selectAll();
        });
    }

    /**
     * 新增分销推广图片
     * @param req
     * @return
     */
    @Override
    public Object addImage(AddImageReq req) {
        if (ObjectUtil.isEmpty(req)){
            //新增
            DistributorImage distributorImage = new DistributorImage();
            distributorImage.setUrl(req.getUrl());
            distributorImage.setDescribes(req.getDescribes());
            distributorImage.setSort(req.getSort());
            distributorImage.setStatus(req.getStatus());
            distributorImage.setDisable(false);
            distributorImage.setCreateTime(new Date());
            this.distributorImageMapper.insert(distributorImage);
        }else {
            //修改
            DistributorImage distributorImage = distributorImageMapper.selectById(req.getId());
            if (ObjectUtil.isEmpty(distributorImage)){
                throw new ApiException(500,"图片不存在");
            }
            distributorImage.setUrl(req.getUrl());
            distributorImage.setSort(req.getSort());
            distributorImage.setStatus(req.getStatus());
            distributorImage.setUpdateTime(new Date());
            distributorImageMapper.updateById(distributorImage);
        }
        return true;
    }

}




